/**
 * <strong>Title : RunningRecordController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.em.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年11月13日 下午3:01:40<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.em.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.RunningRecord;
import cn.b.sky.mes.em.model.condition.RunningRecordCondition;
import cn.b.sky.mes.em.service.RunningRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;
import cn.b.sky.user.service.NumberRuleService;

/**
 * <strong>Title : RunningRecordController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.controller<br></strong>
 * <strong>Description : </strong>设备运行记录控制器<br> 
 * <strong>Create on : 2015年11月13日 下午3:01:40<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/runningRecord/*")
public class RunningRecordController extends SecurityController {

  @Resource
  RunningRecordService runningRecordService;

  @Resource
  NumberRuleService numberRuleService;

  @RequestMapping("runningRecord_list")
  public String selectRunningRecordList(String navTabId, Model model,
      @ModelAttribute("condition") RunningRecordCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<RunningRecord> list = runningRecordService.selectRunningRecordListByCondition(condition);
    int count = runningRecordService.selectRunningRecordCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "em/runningRecord/runningRecord_list";
  }

  @RequestMapping("addRunningRecord")
  public String addRunningRecord(String navTabId, Model model, String id) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "add");
    return "em/runningRecord/runningRecord_add";
  }

  @RequestMapping("modifyRunningRecord")
  public String modifyRunningRecord(String navTabId, Model model, String id) {
    RunningRecord runningRecord = runningRecordService.getRunningRecord(Long.valueOf(id));
    model.addAttribute("runningRecord", runningRecord);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "modify");
    return "em/runningRecord/runningRecord_add";
  }

  @RequestMapping("validateRunningRecordCode")
  @ResponseBody
  public boolean validateRunningRecordCode(String id) {
    if (StringUtils.isEmpty(id)) {
      return true;
    }
    RunningRecord runningRecord = runningRecordService.getRunningRecord(Long.valueOf(id));
    return runningRecord == null;
  }

  @RequestMapping("viewRunningRecord")
  public String viewRunningRecord(String navTabId, Model model, String id) {
    RunningRecord cause = runningRecordService.getRunningRecord(Long.valueOf(id));
    model.addAttribute("runningRecord", cause);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "read");
    return "em/runningRecord/runningRecord_view";
  }

  @RequestMapping("saveRunningRecord")
  @ResponseBody
  public DWZResult saveRunningRecord(String navTabId, Model model, String id, RunningRecord runningRecord) {
    if (runningRecord == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    setCreateInfo(runningRecord);
    runningRecord.setSiteCode(getSiteCode());
    if (StringUtils.isBlank(id)) {
      runningRecordService.insertRunningRecord(runningRecord);
    } else {
      runningRecordService.updateRunningRecord(runningRecord);

    }
    return dialogAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, null, null);

  }

  @RequestMapping("deleteRunningRecord")
  @ResponseBody
  public DWZResult deleteRunningRecord(HttpServletRequest request, String navTabId) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] runningRecordIds = ids.split(",");
    int len = runningRecordIds.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      reports[i] = runningRecordService.deleteRunningRecord(Long.valueOf(runningRecordIds[i]), getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

}
